<template>
  <div id="home">
    <el-container>
      <el-aside :width="asideWidth">
        <el-menu :default-active="defaultActive" :collapse="collapse" router :collapse-transition="false" style="height: 100%">
          <el-menu-item index="/dashboard">
            <i class="el-icon-s-home"></i>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/order/list">
            <i class="el-icon-s-order"></i>
            <span>订单列表</span>
          </el-menu-item>
          <el-submenu index="group">
            <template slot="title">
              <i class="el-icon-s-shop"></i>
              <span>团购活动</span>
            </template>
            <el-menu-item index="/group/add">
              <i class="el-icon-circle-plus"></i>
              <span>马上开团</span>
            </el-menu-item>
            <el-menu-item index="/group/list">
              <i class="el-icon-s-grid"></i>
              <span>团购列表</span>
            </el-menu-item>
          </el-submenu>
          <el-menu-item index="/funds">
            <i class="el-icon-s-finance"></i>
            <span>资金中心</span>
          </el-menu-item>
          <el-submenu index="product">
            <template slot="title">
              <i class="el-icon-shopping-cart-2"></i>
              <span>商品管理</span>
            </template>
            <el-menu-item index="/product/add">
              <i class="el-icon-circle-plus"></i>
              <span>添加商品</span>
            </el-menu-item>
            <el-menu-item index="/product/list">
              <i class="el-icon-s-grid"></i>
              <span>商品列表</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header id="header">
          <i v-if="collapse" style="font-size: 22px" class="el-icon-s-unfold" @click="changeCollapse()"/>
          <i v-if="!collapse" style="font-size: 22px" class="el-icon-s-fold" @click="changeCollapse()"/>
          <div id="avatar">
            <el-dropdown trigger="click" @command="handleCommand">
              <el-avatar :size="40" :src="circleUrl"/>
              <i class="el-icon-caret-bottom"></i>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="logout">登出</el-dropdown-item>
                <el-dropdown-item command="personalCenter">个人中心</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>
        <el-main>
          <router-view/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>

export default {
  name: 'Home',
  data() {
    return {
      defaultActive: '/dashboard',
      collapse: false,
      asideWidth: '175px',
      circleUrl: 'https://www.gx8899.com/uploads/allimg/2017110610/b1m0bva2ptb.jpg'
    }
  },
  methods: {
    changeCollapse() {
      this.collapse = !this.collapse
      if (this.collapse) {
        this.asideWidth = '65px'
      } else {
        this.asideWidth = '200px'
      }
    },
    handleCommand(commend) {
      switch (commend) {
        case 'logout':
          this.logout()
          break
        case 'personalCenter':
          this.$router.push({ path: '/' })
          break
        default:
          console.log('没有选择')
      }
    },
    logout() {
      localStorage.removeItem('jwt')
      this.$router.push({ path: '/login' })
      this.$message.success('您已安全退出!')
    }
  }
}
</script>

<style scoped>
.el-icon-s-fold {
  position: relative;
  left: -15px;
}

.el-icon-s-unfold {
  position: relative;
  left: -15px;
}

#header {
  position: relative;
  text-align: left;
  margin: 0;
  line-height: 60px;
  width: 100%;
  padding: 0 20px;
  border-bottom: 1px solid rgba(32, 33, 30, 0.23);
}

.el-main {
  padding: 0;
}
#avatar {
  float: right;
  position: relative;
  top: 8px;
  right: 40px;
}
.el-icon-caret-bottom {
  position: relative;
  top: -10px;
  left: 5px;
}
</style>
